package ar.edu.itba.jMEBowling;

import ar.edu.itba.jMEBowling.levels.Easy;
import ar.edu.itba.jMEBowling.levels.Hard;
import ar.edu.itba.jMEBowling.levels.Level;
import ar.edu.itba.jMEBowling.levels.Medium;

import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Text;

public class GameMenu {

	private Text play;
	private Text level;
	private Text details;
	private Text help;
	private Text quit;
	private Text[] options = new Text[5];
	private Text helpText1;
	private Text helpText2;
	private Text helpText3;
	private Node statNode;
	private Text finalResult;
	private Runner game;
	private int internalState = 0;
	private long lastUpdatedTime = System.currentTimeMillis();
	// milliseconds to await after updating the menu status
	private long delta = 100;
	private int levelNum = 0;
	private int detailsNum = 0;
	private int screenWidth;
	private int screenHeight;

	public GameMenu(Node statNode, int screenWidth, int screenHeight,
			Runner game) {
		this.statNode = statNode;
		this.game = game;
		this.screenHeight = screenHeight;
		this.screenWidth = screenWidth;
		play = Text.createDefaultTextLabel("play");
		level = Text.createDefaultTextLabel("level");
		details = Text.createDefaultTextLabel("details");
		help = Text.createDefaultTextLabel("help");
		quit = Text.createDefaultTextLabel("quit");
		helpText1 = Text.createDefaultTextLabel("helpText1");
		helpText2 = Text.createDefaultTextLabel("helpText2");
		helpText3 = Text.createDefaultTextLabel("helpText3");

		play.setLocalScale(2f);
		level.setLocalScale(2f);
		details.setLocalScale(2f);
		help.setLocalScale(2f);
		quit.setLocalScale(2f);
		helpText1.setLocalScale(1.3f);
		helpText2.setLocalScale(1.3f);
		helpText3.setLocalScale(1.3f);

		play.setLocalTranslation(screenWidth / 2 - 30, (screenHeight / 2) + 80,
				0);
		level.setLocalTranslation(screenWidth / 2 - 30,
				(screenHeight / 2) + 40, 0);
		details
				.setLocalTranslation(screenWidth / 2 - 30, (screenHeight / 2),
						0);
		help.setLocalTranslation(screenWidth / 2 - 30, (screenHeight / 2) - 40,
				0);
		quit.setLocalTranslation(screenWidth / 2 - 30, (screenHeight / 2) - 80,
				0);

		helpText1.setLocalTranslation(0, screenHeight / 2, 0);
		helpText2.setLocalTranslation(0, screenHeight / 2 - 30, 0);
		helpText3.setLocalTranslation(0, screenHeight / 2 - 60, 0);

		play.setTextColor(ColorRGBA.blue);
		level.setTextColor(ColorRGBA.green);
		details.setTextColor(ColorRGBA.green);
		help.setTextColor(ColorRGBA.green);
		quit.setTextColor(ColorRGBA.green);
		helpText1.setTextColor(ColorRGBA.red);
		helpText2.setTextColor(ColorRGBA.red);
		helpText3.setTextColor(ColorRGBA.red);

		options[0] = play;
		options[1] = level;
		options[2] = details;
		options[3] = help;
		options[4] = quit;

	}

	public void showAllOptions() {

		play.print("Play!");
		statNode.attachChild(play);
		if (levelNum == 0) {
			level.print("Level: Easy  >>");
		}
		if (levelNum == 1) {
			level.print("Level: <<  Medium  >>");
		}
		if (levelNum == 2) {
			level.print("Level: <<  Hard");
		}
		statNode.attachChild(level);
		details.print("Details: Low  >>");
		statNode.attachChild(details);
		help.print("Help");
		statNode.attachChild(help);
		quit.print("Quit");
		statNode.attachChild(quit);
		helpText1
				.print("Durante el juego, debes seleccionar los distintos angulos de la barra con la barra espaciadora.\n");
		helpText2
				.print("Puedes mover la camera utilizando las flechas de direccion y A, W, S y D. En cualquier momento ");
		helpText3
				.print("puedes apretar M para volver al MENU o ESC para salir del juego. Para salir de este Help, presiona Q.");
	}

	public void hideAllOptions() {

		if (statNode.hasChild(play)) {
			statNode.detachChild(play);
			statNode.detachChild(level);
			statNode.detachChild(details);
			statNode.detachChild(help);
			statNode.detachChild(quit);
		}
	}

	private void setMenu() {

	}

	public void keyUp() {

		long now = System.currentTimeMillis();
		if ((now - lastUpdatedTime) > delta) {
			// change the old selected option to white
			options[internalState].setTextColor(ColorRGBA.green);

			if (internalState > 0)
				internalState--;
			else
				internalState = 4;

			// change the new selected option to red
			options[internalState].setTextColor(ColorRGBA.blue);
			lastUpdatedTime = now;
		}

	}

	public void keyDown() {

		long now = System.currentTimeMillis();
		if ((now - lastUpdatedTime) > delta) {
			// change the old selected option to white
			options[internalState].setTextColor(ColorRGBA.green);

			if (internalState < 4)
				internalState++;
			else
				internalState = 0;

			// change the new selected option to red
			options[internalState].setTextColor(ColorRGBA.blue);
			lastUpdatedTime = now;
		}

	}

	public void keyLeft(ShotSpecification shotSpecification) {
		if (internalState == 1) {
			if (levelNum == 1) {
				level.print("Level: Easy  >>");
				levelNum = 0;
				if (shotSpecification != null) {
					shotSpecification.reset();
				}
			}
			if (levelNum == 2) {
				level.print("Level: <<  Medium  >>");
				levelNum = 1;
				if (shotSpecification != null) {
					shotSpecification.reset();
				}
			}
		}
		if (internalState == 2) {
			if (detailsNum == 1) {
				details.print("Details: Low  >>");
				detailsNum = 0;
				game.getScenario().generateLowDetails();
			}
		}
	}

	public void keyRight(ShotSpecification shotSpecification) {
		if (internalState == 1) {
			if (levelNum == 0) {
				level.print("Level: <<  Medium  >>");
				levelNum = 1;
				if (shotSpecification != null) {
					shotSpecification.reset();
				}
			} else if (levelNum == 1) {
				level.print("Level: <<  Hard");
				levelNum = 2;
				if (shotSpecification != null) {
					shotSpecification.reset();
				}
			}
		}
		if (internalState == 2) {
			if (detailsNum == 0) {
				details.print("Details: <<  High");
				detailsNum = 1;
				game.getScenario().generateHighDetails();
			}
		}
	}

	public GameStatus keyEnter() {
		if (internalState == 0) {
			Level level = null;
			switch (levelNum) {
			case 0:
				level = new Easy();
				break;
			case 1:
				level = new Medium();
				break;
			case 2:
				level = new Hard();
				break;
			default:
				level = new Medium();
			}
			if (detailsNum == 1) {
				game.setAllDetails(true);
			}
			if (game.getShotSpecification() == null) {
				game.createShotSpecification(level);
			} else {
				game.changeShotLevel(level);
			}
			if (detailsNum == 1) {
				game.getScenario().generateHighDetails();
			}
			hideAllOptions();
			game.enableCamera();
			if (game.isInPause()) {
				game.pause();
				game.setInPause(false);
				game.enableCamera();
				return game.getBeforePause();
			}
			return GameStatus.WAITINGXPOS;
		}
		if (internalState == 3) {
			hideAllOptions();
			statNode.attachChild(helpText1);
			statNode.attachChild(helpText2);
			statNode.attachChild(helpText3);
			return GameStatus.INHELP;
		}
		if (internalState == 4) {
			System.exit(0);
		}
		return GameStatus.INMENU;
	}

	public GameStatus keyQ() {
		showAllOptions();
		statNode.detachChild(helpText1);
		statNode.detachChild(helpText2);
		statNode.detachChild(helpText3);
		return GameStatus.INMENU;
	}

	public void showAllOptions(int finalResults) {
		finalResult = Text.createDefaultTextLabel("finalResults");
		finalResult.setLocalScale(3f);
		finalResult.setLocalTranslation(screenWidth / 2 - 100,
				(screenHeight / 2) + 200, 0);
		finalResult.setTextColor(ColorRGBA.blue);
		finalResult.print("You scored " + finalResults + " points");
		statNode.attachChild(finalResult);
		showAllOptions();
	}

}
